package com.timi_licai.timi_licai_account.dialog;

import android.app.Dialog;
import android.content.ContentValues;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;

import com.timi_licai.timi_licai_account.MainActivity;
import com.timi_licai.timi_licai_account.MyConstants;
import com.timi_licai.timi_licai_account.R;
import com.timi_licai.timi_licai_account.adapter.AddAccountBookBGAdapter;
import com.timi_licai.timi_licai_account.database.DatabaseUtils;
import com.timi_licai.timi_licai_account.model.BookBg;
import com.timi_licai.timi_licai_account.utils.DateUtils;

import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * 添加账本的dialog
 */
public class AddCountDialog extends Dialog implements View.OnClickListener {
    private EditText editText;
    private Button btnCancel;
    private Button btnConfirm;
    private Context context;
    private List<BookBg> bgColor;
    private AddAccountBookBGAdapter bookBGAdapter;

    public AddCountDialog(Context context) {
        super(context);
        this.context = context;
    }

    public AddCountDialog(Context context, int theme) {
        super(context, theme);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_add_account_book);
        bgColor = new LinkedList<>();
        BookBg bg = new BookBg();
        bg.setColor("#FFB5BA3F");
        bg.setSelected(true);
        bgColor.add(bg);
        bg = new BookBg();
        bg.setColor("#FFFDC402");
        bg.setSelected(false);
        bgColor.add(bg);
        bg = new BookBg();
        bg.setColor("#FFEB8BBE");
        bg.setSelected(false);
        bgColor.add(bg);
        bg = new BookBg();
        bg.setColor("#FFB5BA3F");
        bg.setSelected(false);
        bgColor.add(bg);
        bg = new BookBg();
        bg.setColor("#FFB5BA3F");
        bg.setSelected(false);
        bgColor.add(bg);

        editText = (EditText) findViewById(R.id.dialog_add_account_book_input_txt);
        btnCancel = (Button) findViewById(R.id.dialog_add_account_book_btn_cancel);
        btnConfirm = (Button) findViewById(R.id.dialog_add_account_book_btn_confirm);
        btnCancel.setOnClickListener(this);
        btnConfirm.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.dialog_add_account_book_btn_cancel:
                // 点击了取消按钮
                this.cancel();
                break;
            case R.id.dialog_add_account_book_btn_confirm:
                //点击了确定按钮
                String inputText = editText.getText().toString().trim();
                if (!TextUtils.isEmpty(inputText)) {
                    ContentValues values = new ContentValues();
                    Date currentDate = Calendar.getInstance().getTime();
                    String date = DateUtils.formatDateForDatabase(currentDate);
                    values.put("bookname", inputText);
                    values.put("createtime", date);
                    DatabaseUtils.insert(MyConstants.TABLE_ACCOUNT_BOOK, values);
                }
                if (context instanceof MainActivity) {
                    MainActivity mainActivity = (MainActivity) context;
                    mainActivity.flushCountBook();
                }
                this.cancel();
                break;
        }
    }

}
